﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 13.09.2013
 * Time: 10:13
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Region
{
	/// <summary>
	/// Description of DBForm.
	/// </summary>
	public interface IDBForm 
	{
		ObjectFormUtil i_reader { get; }
		DBObject i_object { get; }
		/*void readObject();
		void saveObject();
		void checkObject();*/
	}
	public static class DBForm
	{
	  /// <summary>Добавляет редактор если его имя совпадает с именем поля</summary><param name="Form">Форма</param><param name="editor">Редактор</param><returns>true  если редактор добавлен</returns>
	  public static bool addEditor(IDBForm Form, Control editor,Object ExProcessor=null)
	  {
	    ObjectFormUtil i_reader = Form.i_reader;
	    if (!Form.i_object.isPole(editor.Name)) 
	      return false;
	    i_reader.AddEditor(editor, editor.Name,ExProcessor);	       
	    return true;
	  }
	  /// <summary>Добавляет редактор</summary><param name="Form">Форма</param><param name="Name">Имя поля</param><param name="editor">Редактор</param><returns>true  если редактор добавлен</returns>
	  public static bool addEditor(IDBForm Form, String Name, Control editor,Object ExProcessor=null)
	  {
	    ObjectFormUtil i_reader = Form.i_reader;
	    if (!Form.i_object.isPole(Name)) return false;
	    i_reader.AddEditor(editor, Name,ExProcessor);	       
	    return true;
	  }
	  public static int addEditors(IDBForm Form, Control parent,Dictionary<String,Object> ListExProcessor=null)
		{
			ObjectFormUtil i_reader = Form.i_reader;
			DBObject i_object = Form.i_object;
			Int32 numEditors=0;
			foreach (Control editor in parent.Controls) 
			{
			  Object ExProcessor = ListExProcessor!=null&&ListExProcessor.ContainsKey(editor.Name)?ListExProcessor[editor.Name]:null;
			  if (addEditor(Form, editor, ExProcessor))
			    numEditors += 1;
			  else
			    numEditors += addEditors(Form, editor, ListExProcessor);
			}
			return numEditors;
		}
	}
}
